package com.base.cn.platform.os.controller.integralgood;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.integralgood.IntegralGood;
import com.base.cn.platform.os.entity.integralgood.IntegralShopType;
import com.base.cn.platform.os.entity.integralgood.IntegralVirtualGood;
import com.base.cn.platform.os.entity.op.coupon.OpCoupon;
import com.base.cn.platform.os.entity.op.giftcard.OpGiftCard;
import com.base.cn.platform.os.service.integralgood.IntegralGoodBiz;
import com.base.cn.platform.os.service.integralgood.IntegralVirtualGoodBiz;
import com.base.cn.platform.os.service.op.coupon.OpCouponBiz;
import com.base.cn.platform.os.service.op.giftcard.OpGiftCardBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 积分兑换商品
 * @Author: Hello豆包
 * @Date: 2019/10/18 14:43
 */
@RestController
@RequestMapping("/manage/integralGood/")
public class IntegralGoodController extends BaseController {

        @Autowired
        private IntegralGoodBiz integralGoodBiz;
        @Autowired
        private OpCouponBiz opCouponBiz;
        @Autowired
        private OpGiftCardBiz opGiftCardBiz;
        @Autowired
        private IntegralVirtualGoodBiz integralVirtualGoodBiz;
        @RequestMapping("saveEntity")
        public Map saveEntity(@RequestBody IntegralGood entity){
                if(StringUtils.isEmpty(entity.getName())){
                        return  ResultUtil.ERROR("商品名称不能为空");
                }
                if(entity.getName().length()>60){
                        return  ResultUtil.ERROR("商品名称不能大于60个字符");
                }
                if(ObjectUtils.isEmpty(entity.getIntegral())){
                        return  ResultUtil.ERROR("请填项兑换商品需要的积分");
                }
                if(!DataUtil.idIsNotNull(entity.getId())&&entity.getIntegral()>999999999){
                        return  ResultUtil.ERROR("兑换商品的最大积分不能超过999999999");
                }
                if(!DataUtil.idIsNotNull(entity.getId())&&ObjectUtils.isEmpty(entity.getInventory())){
                        return  ResultUtil.ERROR("请填写商品的库存");
                }
                if(!DataUtil.idIsNotNull(entity.getId())&&entity.getInventory()>999999999){
                        return  ResultUtil.ERROR("兑换商品的最大库存不能超过999999999");
                }
                if(!DataUtil.idIsNotNull(entity.getId())&&ObjectUtils.isEmpty(entity.getType())){
                        return  ResultUtil.ERROR("请选择商品的形态");
                }
                if(StringUtils.isEmpty(entity.getDetails())){
                        return  ResultUtil.ERROR("商品介绍不能为空");
                }
                if(StringUtils.isEmpty(entity.getImageJson())){
                        return  ResultUtil.ERROR("请上传图片");
                }
                if(entity.getShopType().equals(IntegralShopType.COUPON)||entity.getShopType().equals(IntegralShopType.COURSE)||entity.getShopType().equals(IntegralShopType.GIFT_CARD)){
                   //设置是否为平台商品
                    entity.setPlatformShop(1);
                }
                if(DataUtil.idIsNotNull(entity.getId())){
                    IntegralGood byId = integralGoodBiz.findById(entity.getId());
                    if(ObjectUtils.isNotEmpty(entity.getType())&&!byId.getType().equals(entity.getType())){
                        return  ResultUtil.ERROR("商品形态不可修改");
                    }
                    if(ObjectUtils.isNotEmpty(entity.getInventory())&&!byId.getInventory().equals(entity.getInventory())&&byId.getStatus()==3&&!entity.equals(IntegralShopType.COURSE)&&!entity.equals(IntegralShopType.ENTITY)){
                        return  ResultUtil.ERROR("库存不可修改");
                    }
                    integralGoodBiz.updateById(entity,null);
                }else{


                    integralGoodBiz.save(entity);
                    if(!entity.getShopType().equals(IntegralShopType.ENTITY)){
                        //设置是否为平台商品
                        dealWith(entity);
                     }
                }
                 return ResultUtil.SUCCESS(entity);
        }

        /**
         * 查询分页列表
         * @param currentPage
         * @param integralGood
         * @return
         */
            @RequestMapping("findListPage")
            public PageInfo findListPage(@RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage , @RequestBody IntegralGood integralGood){

                                Pagination page = new Pagination();
                                page.setCurrentPage(currentPage);

                        return integralGoodBiz.findListPage(page,integralGood);
                }

        /**
         * 更新状态
         * @param ids
         * @param status
         * @return
         */
         @RequestMapping("updateStatus")
          public  Map updateStatus(@RequestParam("ids") String ids,@RequestParam("status") Integer status){
                if (StringUtils.isEmpty(ids)){
                        return  ResultUtil.ERROR("请选择数据");
                }
                 if (ObjectUtils.isEmpty(status)||status<0||status>4){
                         return  ResultUtil.ERROR("错误操作，不可修改为该状态");
                 }
                 IntegralGood integralGood = new IntegralGood();
                 integralGood.setStatus(status);
                 ids = StringUtils.subHeadTailString(ids, ",");
                 integralGoodBiz.updateByWhereSql(integralGood," id in("+ids+")");
                 return  ResultUtil.SUCCESS("修改成功");
        }


    /**
     * 根据Id查询
     * @param id
     * @return
     */
    @RequestMapping("findByIdEntity")
    public  IntegralGood  findByIdEntity(@RequestParam(value = "id") BigDecimal id,@RequestParam(value = "isSetShopData") boolean isSetShopData){
      return   integralGoodBiz.findById(id,isSetShopData);
    }

    /**
     * 兑换商品
     * @param userId
     * @param shopId
     * @return
     */
    @RequestMapping("exchangeIntegralShop")
    public Map  exchangeIntegralShop(@RequestParam(value = "userId") BigDecimal userId,@RequestParam(value = "shopId") BigDecimal shopId){
        return   integralGoodBiz.exchangeIntegralShop(userId,shopId);
    }


    @RequestMapping("redeemedCash")
    public  Map redeemedCash(@RequestParam("userId") BigDecimal userId,@RequestParam Integer cash){

        return   integralGoodBiz.redeemedCash(userId,cash);
    }


    private void dealWith(IntegralGood entity) {


        switch (entity.getShopType()) {

            case IntegralShopType.VIRTUAL_GOODS:
                IntegralVirtualGood integralVirtualGood = new IntegralVirtualGood();
                integralVirtualGood.setId(entity.getShopId());
                integralVirtualGood.setIntegralGoodId(entity.getId());
                integralVirtualGood.setStatus(3);
                integralVirtualGoodBiz.updateById(integralVirtualGood, null);
                break;
            case IntegralShopType.COUPON:
                OpCoupon opCoupon = new OpCoupon();
                opCoupon.setId(entity.getShopId());
                opCoupon.setIntegralGoodId(entity.getId());
                opCouponBiz.updateById(opCoupon, null);
                break;

            case IntegralShopType.GIFT_CARD:
                OpGiftCard opGiftCard = new OpGiftCard();
                opGiftCard.setId(entity.getShopId());
                opGiftCard.setIntegralGoodId(entity.getId());
                opGiftCardBiz.updateById(opGiftCard, null);
                break;
            default:
                break;

        }


    }
}
